-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: exclude cache tokens from context window calculation #5603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Modified getApiMetrics.ts to calculate contextTokens as tokensIn + tokensOut only - Updated tests to reflect the new calculation behavior - Cache tokens (cacheWrites and cacheReads) no longer count towards context window - Fixes inflated context token display in UI that was showing ~170k instead of ~44k
|
✅ No security or compliance issues detected. Reviewed everything up to 62c9d5d. Security Overview
Detected Code Changes
Reply to this PR with |
Collaborator
|
Hmm, I'm confused - I thought that these were additive, at least for some providers. Maybe we're just handling openrouter incorrectly now since we added cached tokens to it? |
- Modified claude-code.ts to yield incremental token usage after each assistant message - This ensures the UI receives real-time token updates during streaming - Prevents accumulation issues in Task.ts which was causing incorrect totals - Fixes #5600 where assistant messages showed incorrect low token counts
mrubens
reviewed
Jul 11, 2025
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
mrubens
approved these changes
Jul 11, 2025
mrubens
approved these changes
Jul 11, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
Something isn't working
Issue/PR - Triage
New issue. Needs quick review to confirm validity and assign labels.
lgtm
This PR has been approved by a maintainer
size:M
This PR changes 30-99 lines, ignoring generated files.
UI/UX
UI/UX related or focused
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes the token counting discrepancy where the UI was displaying inflated context token counts (e.g., ~170k instead of ~44k) because cache tokens were being included in the context window calculation.
Problem
The function was calculating as:
However, cache tokens do not consume actual context window space - they are a caching optimization. Including them in the context window display was misleading users about how much of their context window was actually being used.
Solution
Modified the calculation to only include actual context tokens:
Changes
src/shared/getApiMetrics.tsto exclude cache tokens from thecontextTokenscalculationsrc/shared/__tests__/getApiMetrics.spec.tsto reflect the new behaviorTesting
Impact
This change only affects the UI display of context tokens. It does not impact:
The fix ensures users see an accurate representation of their actual context window usage.
Important
Fixes token counting in
getApiMetrics.tsby excluding cache tokens for OpenAI protocol and updates related tests.getApiMetrics.tsby excluding cache tokens fromcontextTokensfor OpenAI protocol.getApiMetrics()to calculatecontextTokensbased onapiProtocol.getApiMetrics.spec.tsto reflect newcontextTokenscalculation logic.apiProtocolhandling inTask.tsto determine protocol based on provider.getApiProtocol()function inprovider-settings.tsto determine API protocol.This description was created by
for 62c9d5d. You can customize this summary. It will automatically update as commits are pushed.